package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "预约管理")
@RestController
@RequestMapping("/customer/reservation")

public class CustomerReservationController extends BaseController {

    @Autowired
    private CustomerReservationService customerReservationService;

    /**
     * 查询取消预约数量
     */
    @GetMapping("/cancelled-count")
    @ApiOperation(value = "查询取消预约数量")
    public ResponseResult getCancelledCount() {
        Long userId = UserThreadLocal.getUserId();

        return success(customerReservationService.getCancelledCount(userId));
    }

    /**
     * 查询每个时间段剩余预约次数
     */
    @GetMapping("/countByTime")
    @ApiOperation(value = "查询每个时间段剩余预约次数")
    public List<TimeCountVo> getReservationCount(Long time) {
        List<TimeCountVo> list = new ArrayList<>();

        LocalDateTime beginOfDay = LocalDateTimeUtil.of(time);
        LocalDateTime current = LocalDateTime.now();
        LocalDateTime during = current.plusDays(1);
        LocalDateTime startTime = beginOfDay.plusHours(8);
        LocalDateTime endTime = startTime.plusHours(10);

        /*// 当前时间大于预约结束时间，则从明天开始计算
        if (current.isAfter(endTime)) {
            startTime = startTime.plusDays(1);
            endTime = startTime.plusHours(10);
        }
        // 当前时间介于二者之间，则从当前时间开始到当天结束时间，以及第二天开始时间到持续时间结束
        if (current.isAfter(startTime) && current.isBefore(endTime)) {
            startTime = current;
        }*/

        // 当前时间小于预约开始时间，则从预约开始时间开始计算
        // while循环，半小时增加
        while (!startTime.isAfter(endTime)) {
            int count = customerReservationService.getReservationCount(startTime, startTime.plusMinutes(30));

            TimeCountVo timeCountVo = new TimeCountVo();
            timeCountVo.setStartTime(startTime);
            timeCountVo.setCount(6 - count);

            // 添加到结果列表中
            list.add(timeCountVo);

            // 移动到下一个时间段
            startTime = startTime.plusMinutes(30);
        }
        return list;
    }

    /**
     * 新增预约
     */
    @PostMapping
    @ApiOperation(value = "新增预约")
    public ResponseResult addReservation(@ApiParam(value = "预约数据传输对象", required = true)
                                         @RequestBody ReservationDto reservationDto) {

        customerReservationService.addReservation(reservationDto);
        return ResponseResult.success();
    }

    /**
     * 分页查询当前登录用户的预约信息
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询当前登录用户的预约信息")
    public ResponseResult<PageResponse> selectByPage(
            Integer pageNum,
            Integer pageSize,
            Integer status
    ) {
        Long userId = UserThreadLocal.getUserId();
        return success(customerReservationService.selectByPage(pageNum, pageSize, userId, status));
    }

    /**
     * 取消预约
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation(value = "取消预约")
    public void cancelReservationByHand(@PathVariable("id") Long id) {
        customerReservationService.cancelReservation(id);
    }


}
